package com.whz.lambdademo;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Comparator.*;

/**
 * @Author: sinosoft
 * @Description: 通过匿名类创建函数对象
 * @Date: Create in 18:14 2021/5/17
 * @Modified By:
 */
@Slf4j
public class MainTest {
    public static void main(String[] args) {
        String[] strArr = {"IDEA", "中", "如何", "给main", "方法附带参数", "_星夜小筑的专栏-CSDN博客"};
        List<String> words = Arrays.stream(strArr).collect(Collectors.toList());

        log.info("原始的字符串列表: {}", words);
        Collections.sort(words, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.compare(o1.length(), o2.length());
            }
        });
        log.info("经过排序的字符串列表: {}", words);

        // 使用Lambda消除样板代码
        Collections.sort(words,
                (s1, s2) -> Integer.compare(s1.length(), s2.length()));
        Collections.sort(words,
                comparingInt(String::length));
        words.sort(comparingInt(String::length));
    }
}
